package edu.bvrcj.profile;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
 
@Configuration
public class DemoJavaConfig {
 
  @Value("${hellow.world.name}") String helloWorldName;
 
  @Bean
  public HelloWorldBean helloWorldBean() {
    return new HelloWorldBean(helloWorldName);
  }
   
  /**
   * Add PropertySourcesPlaceholderConfigurer to make placeholder work.
   * This method MUST be static
   */
  @Bean
  @Profile("dev")
  public static PropertySourcesPlaceholderConfigurer devPropertyConfigurer() {
    Resource resource = new ClassPathResource("development.properties");

    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer =  new PropertySourcesPlaceholderConfigurer();

    // load the property file
    propertySourcesPlaceholderConfigurer.setLocation(resource);
     
    return propertySourcesPlaceholderConfigurer;
  }

  @Bean
  @Profile("test")
  public static PropertySourcesPlaceholderConfigurer testPropertyConfigurer() {
    Resource resource = new ClassPathResource("test.properties");

    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer =  new PropertySourcesPlaceholderConfigurer();

    // load the property file
    propertySourcesPlaceholderConfigurer.setLocation(resource);

    return propertySourcesPlaceholderConfigurer;
  }

  @Bean
  @Profile("prod")
  public static PropertySourcesPlaceholderConfigurer prodPropertyConfigurer() {
    Resource resource = new ClassPathResource("prod.properties");

    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer =  new PropertySourcesPlaceholderConfigurer();

    // load the property file
    propertySourcesPlaceholderConfigurer.setLocation(resource);

    return propertySourcesPlaceholderConfigurer;
  }
}